package org.liquidByte.trafficMustang.androidApp.listview.row;

import java.security.InvalidParameterException;

import org.liquidByte.trafficMustang.R;
import org.liquidByte.trafficMustang.androidApp.listview.adapter.RowAdapter;
import org.liquidByte.trafficMustang.androidApp.services.dto.AvaliacaoDTO;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class AvaliacaoRow extends ModelListViewRow<AvaliacaoDTO> implements OnClickListener {

	private TextView txtAvaliado;
	private ImageButton btnRating;
	private ImageButton btnConfirm;
	
	private RowActionListener<AvaliacaoRow> confirmarActionListener;
	private RowActionListener<AvaliacaoRow> editarActionListener;
	
	public AvaliacaoRow(RowAdapter<AvaliacaoDTO> parentAdapter, View view) {
		super(parentAdapter, view);
		if (view.getId() != R.layout.listview_row_avaliacao) {
			throw new InvalidParameterException("view is not a type of listview_row_avaliacao");
		}
	}
	
	public TextView getTxtAvaliado() {
		if (this.txtAvaliado == null) {
			this.txtAvaliado = (TextView) this.getView().findViewById(R.id.row_avaliacao_txtAvaliado);
		}
		return txtAvaliado;
	}
	
	public ImageButton getBtnRating() {
		if (this.btnRating == null) {
			this.btnRating = (ImageButton)this.getView().findViewById(R.id.row_avaliacao_rating);
		}
		return btnRating;
	}

	public ImageButton getBtnConfirm() {
		if (this.btnConfirm == null) {
			this.btnConfirm = (ImageButton)this.getView().findViewById(R.id.row_avaliacao_confirm);
		}
		return btnConfirm;
	}
	
	public void setConfirmarActionListener(RowActionListener<AvaliacaoRow> listener) {
		this.confirmarActionListener = listener;
	}
	
	public void setEditarActionListener(
			RowActionListener<AvaliacaoRow> editarActionListener) {
		this.editarActionListener = editarActionListener;
	}
	
	public void setValues(String descricao, float rating) {
		AvaliacaoDTO model = this.getModel();
		model.setDescricao(descricao);
		model.setQualificacao((int)(rating * 2 - 3));
		model.setPendente(false);
		this.bindToView();
	}

	@Override
	public void bindToView() {
		AvaliacaoDTO avaliacao = (AvaliacaoDTO) this.getModel();

		if (avaliacao == null) {
			this.getBtnConfirm().setVisibility(View.GONE);
			this.getTxtAvaliado().setText(null);
		} else {
			this.getBtnConfirm().setVisibility(avaliacao.isPendente() ? View.GONE : View.VISIBLE);
			if (avaliacao.getAvaliado() == null) {
				this.getTxtAvaliado().setText(null);
			} else {
				this.getTxtAvaliado().setText(avaliacao.getAvaliado().getNome());
			}
		}
	}

	@Override
	public void init() {
		this.getBtnConfirm().setOnClickListener(this);
		this.getBtnRating().setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (this.getBtnConfirm() == v) {
			if (this.confirmarActionListener != null) {
				this.confirmarActionListener.actionPerformed(this);
			}
		} else if (this.getBtnRating() == v) {
			if (this.editarActionListener != null) {
				this.editarActionListener.actionPerformed(this);
			}
		}
	}
}
